package com.yz.arithmetic;

/**
 * 冒泡排序
 *
 * @author yuanzheng
 * @date 2020/9/24-15:01
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] array = {21, 43, 35, 7, 19, 11, 13};
        bubbleSort(array, array.length);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    public static void bubbleSort(int[] a, int n) {
        int i, j;
        //表示 n 次排序过程
        for (i = 0; i < n; i++) {
            for (j = 1; j < n - i; j++) {
                //前面的数字大于后面的数字就交换
                if (a[j - 1] > a[j]) {
                    int temp;
                    temp = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp;
                }
            }
        }
    }
}
